package xin.key
{
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.utils.Dictionary;

	/**
	 * 热键控制类, 使用方法:
	 * <p/>var hotKey:XHotKey = new XHotKey(this.stage);
	 * <p/>hotKey.active = true;
	 * <p/>hotKey.setHotKey(Keyboard.DELETE, callBack, 1);
	 * 
	 * @author xin
	 */	
	public class XHotKey
	{
		// 舞台
		private var _owner:Stage;
		//键码字典
		private var _dic:Dictionary;
		//热键启用状态
		private var _blActive:Boolean;
		
		/**
		 * 按下键码事件处理
		 */		
		private function keyDownHandler(evt:KeyboardEvent):void
		{
			var keyVo:HotKeyVo = this._dic[evt.keyCode];
			if (keyVo)
			{
				keyVo.onKeyDown();
			}
		}
		
		/**
		 * 按键弹起事件处理
		 */		
		private function keyUpHandler(evt:KeyboardEvent):void
		{
			var keyVo:HotKeyVo = this._dic[evt.keyCode];
			if (keyVo)
			{
				keyVo.onKeyUp();
			}
		}
		
		/**
		 * 当前flash失去活动焦点时事件处理, 触发所有已按下并且相应 弹起的按键事件
		 */		
		private function deactivateHandler(evt:Event):void
		{
			for each (var keyVo:HotKeyVo in this._dic)
			{
				keyVo.onKeyUp();
			}
		}
		
		/**
		 * 构造函数
		 * @param owner 舞台
		 */		
		public function XHotKey(owner:Stage)
		{
			this._owner = owner;
			// 默认不启用热键
			this._blActive = false;
			this._dic = new Dictionary();
		}
		
		/**
		 * 设置热键是否启用
		 */		
		public function set active(value:Boolean):void
		{
			if (this._blActive != value)
			{
				this._blActive = value;
				
				//启用
				if (value)
				{
					this._owner.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
					this._owner.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
					this._owner.addEventListener(Event.DEACTIVATE, deactivateHandler);
				}
				//禁用
				else
				{
					this._owner.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
					this._owner.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
					this._owner.removeEventListener(Event.DEACTIVATE, deactivateHandler);
				}
			}
		}
		
		/**
		 * 重置键码定义
		 */		
		public function reset(keyCode:uint):void
		{
			var keyVo:HotKeyVo = this._dic[keyCode];
			if (keyVo)
			{
				keyVo.reset();
			}
		}
		
		/**
		 * 设置热键
		 * @param callBack 热键对应的回调函数, 如果为null则删除该热键
		 * @param activeMode 启用热键的方式, 0: 按下并弹起时触发, 1:仅按下 ; 参见HotKeyVo定义
		 */		
		public function setHotKey(keyCode:uint, callBack:Function, activeMode:int = 0):void
		{
			if (callBack == null)
			{
				delete this._dic[keyCode];
			}
			else
			{
				this._dic[keyCode] = new HotKeyVo(keyCode, callBack, activeMode);
			}
		}
		
		/**
		 * 判断热键是否按下, 非热键返回false
		 */		
		public function isKeyDown(keyCode:uint):Boolean
		{
			var keyVo:HotKeyVo = this._dic[keyCode];
			if (keyVo)
			{
				return keyVo.isKeyDown;
			}
			return false;
		}
		
		/**
		 * 设置指定键码的激活状态
		 */		
		public function setActive(keyCode:uint, blActive:Boolean):void
		{
			var keyVo:HotKeyVo = this._dic[keyCode];
			if (keyVo)
			{
				keyVo.active = blActive;
			}
		}
		
		/**
		 * 获取指定键码的激活状态
		 */		
		public function getActive(keyCode:uint):Boolean
		{
			var keyVo:HotKeyVo = this._dic[keyCode];
			if (keyVo)
			{
				return keyVo.active;
			}
			return false;
		}
		
	}
}